package com.gmcc.docmanager.loader;

import java.util.Map;

import com.gmcc.docmanager.util.loader.DefaultMakeHttpHead;
import com.gmcc.docmanager.util.loader.UrlLoader;

import android.content.Context;

public class Sender {

	private Context mContext;
	private Request requestObject;
	private ResponseParser responseParser;

	public Sender(Context context, Request requestObject,
			ResponseParser responseParser) {
		this.mContext = context;
		this.requestObject = requestObject;
		this.responseParser = responseParser;
	}

	public void send() {
		UrlLoader loader = UrlLoader.getDefault(mContext);
		loader.loadUrl(requestObject.getRequestUrl(), getPostData(),
				new DefaultMakeHttpHead(), responseParser);
	}

	private String getPostData() {
		if (requestObject == null) {
			return null;
		}
		if (requestObject.getRequestData() == null
				|| requestObject.getRequestData().isEmpty()) {
			return null;
		}
		StringBuffer result = new StringBuffer();
		for (Map.Entry<String, String> entry : requestObject.getRequestData()
				.entrySet()) {
			if (result.length() != 0) {
				result.append("&");
			}
			result.append(entry.getKey()).append("=").append(entry.getValue());
		}
		return result.toString();
	}
}
